// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.dbmanager.ui;

import java.util.List;
import java.util.Map;

import org.dengues.warehouse.i18n.Messages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class ObjectBrowserComposite extends Composite {

    private CTabFolder folder;

    /**
     * Qiang.Zhang.Adolf@gmail.com ObjectBrowserComposite constructor comment.
     * 
     * @param parent
     */
    public ObjectBrowserComposite(Composite parent) {
        super(parent, SWT.NONE);
        setBackground(parent.getBackground());
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginBottom = 0;
        gridLayout.marginWidth = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginHeight = 0;

        setLayout(gridLayout);
        setLayoutData(new GridData(GridData.FILL_BOTH));
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createTabFolder".
     * 
     * @param force
     */
    private void createTabFolder(boolean force) {
        if (folder == null || folder.isDisposed() || force) {
            clearParent();
            folder = new CTabFolder(this, SWT.NULL);
            folder.setSimple(false);
            folder.setToolTipText(Messages.getString("RunResultsComposit.Title.Name")); //$NON-NLS-1$
            GridData gd = new GridData(GridData.FILL_BOTH);
            folder.setLayoutData(gd);
            layout();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "clearParent".
     */
    private void clearParent() {
        Control[] children = this.getChildren();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                children[i].dispose();
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createTabItem".
     * 
     * @param text
     * @param strings
     * @param data
     */
    private void createTabItem(String text, String[] strings, List<String[]> data) {
        CTabItem defalutTabItem = new CTabItem(folder, SWT.NULL);
        defalutTabItem.setText(text);
        Composite defaultComposite = new Composite(folder, SWT.NULL);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        layout.marginLeft = 0;
        layout.marginTop = 0;
        layout.marginBottom = 0;

        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        layout.marginWidth = 0;
        layout.marginHeight = 0;

        defaultComposite.setLayout(layout);
        defaultComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
        defalutTabItem.setControl(defaultComposite);

        Table defaultTable = new Table(defaultComposite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
        defaultTable.setHeaderVisible(true);
        defaultTable.setLinesVisible(true);
        layout = new GridLayout();
        layout.marginLeft = 0;
        layout.marginTop = 0;
        layout.marginBottom = 0;
        layout.horizontalSpacing = 0;
        layout.verticalSpacing = 0;
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        defaultTable.setLayout(layout);
        defaultTable.setLayoutData(new GridData(GridData.FILL_BOTH));

        Label status = new Label(defaultComposite, SWT.NONE);
        status.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        updateDefaultTable(defalutTabItem, status, defaultTable, strings, data);
    }

    private void updateDefaultTable(CTabItem defalutTabItem, Label status, Table defaultTable, String[] colNames,
            List<String[]> data) {
        if (defaultTable != null && defalutTabItem != null) {
            clearDefaultTable(defaultTable);
            refreshDefaultTableHeader(defaultTable, colNames);
            refreshDefaultTableItems(defaultTable, data);
            status.setText(Messages.getString("ObjectBrowserComposite.data.size", data.size())); //$NON-NLS-1$
            // resize all the columns but not the table
            for (int i = 0; i < defaultTable.getColumnCount(); i++) {
                defaultTable.getColumn(i).pack();
            }
            // scroll to show the first col and first row
            if (defaultTable.getItems().length > 0) {
                defaultTable.showItem(defaultTable.getItem(0));
                if (defaultTable.getColumns() != null && defaultTable.getColumns().length > 0) {
                    defaultTable.showColumn(defaultTable.getColumn(0));
                }
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refreshDefaultTableItems".
     * 
     * @param defaultTable
     * @param rows
     */
    private void refreshDefaultTableItems(Table defaultTable, List<String[]> rows) {
        int existingItemCount = defaultTable.getItemCount();
        int end = rows.size();
        for (int f = 0; f < end; f++) {
            if (f >= existingItemCount) {
                // create a new Item
                TableItem row = new TableItem(defaultTable, SWT.NONE);
                row.setText(rows.get(f));
            } else {
                // update an existing Item
                defaultTable.getItem(f).setText(rows.get(f));
            }
        }

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "clearDefaultTable".
     * 
     * @param defaultTable
     */
    private void clearDefaultTable(Table defaultTable) {
        defaultTable.clearAll();
        for (int i = 0; i < defaultTable.getColumnCount(); i++) {
            defaultTable.getColumn(i).setText(""); //$NON-NLS-1$
            defaultTable.getColumn(i).setWidth(0);
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "refreshDefaultTableHeader".
     * 
     * @param defaultTable
     * @param columns
     */
    private void refreshDefaultTableHeader(Table defaultTable, String[] columns) {
        int existingColumnsLength = defaultTable.getColumnCount();
        // update the existing columns
        for (int i = 0; i < existingColumnsLength; i++) {
            if (i < columns.length) {
                // the column exist and must be updated
                defaultTable.getColumn(i).setText(columns[i]);
            } else {
                // the following column exist and must be dispose
                existingColumnsLength = i;
            }
        }

        // if necessary, add the another columns must be created
        for (int i = existingColumnsLength; i < columns.length; i++) {
            TableColumn column = new TableColumn(defaultTable, SWT.LEFT);
            column.setText(columns[i]);
        }

        // if necessary, dispose the unusable columns
        while (defaultTable.getColumnCount() > columns.length) {
            defaultTable.getColumn(defaultTable.getColumnCount() - 1).dispose();
        }

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "updateObjectBrowser".
     * 
     * @param dataMap
     */
    public void updateObjectBrowser(Map<String, List<String[]>> dataMap) {
        clearParent();
        createTabFolder(true);
        for (String title : dataMap.keySet()) {
            List<String[]> list = dataMap.get(title);
            createTabItem(title, list.remove(0), list);
        }
        if (folder.getItemCount() > 0) {
            folder.setSelection(0);
        }
    }
}
